import type { GeoJSONStoreFeatures } from 'terra-draw';
import type { PageServerLoad } from './$types';

export const load: PageServerLoad = async ({ fetch, parent }) => {
	const { metadata, styles } = await parent();

	const getExample = async (type: 'cdn' | 'npm') => {
		if (type === 'cdn') {
			const res = await fetch(`/assets/maplibre-cdn-example.txt`);
			const text = await res.text();
			return text.replace(
				/..\/..\//g,
				`https://cdn.jsdelivr.net/npm/${metadata.packageName}@${metadata.version}/`
			);
		} else {
			const res = await fetch(`/assets/maplibre-npm-example.txt`);
			const text = await res.text();
			return text.replace(/{style}/g, styles[0].uri);
		}
	};

	const geojson: GeoJSONStoreFeatures[] = [
		{
			id: 'ba02a643-a4e3-4dfe-abf7-989c6606a4f6',
			type: 'Feature',
			geometry: {
				type: 'Point',
				coordinates: [-1.415562074, 53.336739372]
			},
			properties: {
				mode: 'point'
			}
		},
		{
			id: '951c9e4b-a3fb-451d-ac32-02c960328f22',
			type: 'Feature',
			geometry: {
				type: 'LineString',
				coordinates: [
					[11.308944771, 41.212881459],
					[16.690948697, 51.421153329],
					[22.296701557, 40.596457608],
					[26.663414621, 51.281412445]
				]
			},
			properties: {
				mode: 'linestring'
			}
		},
		{
			id: '4779c1db-a3ae-48a2-9519-9d4eafec84d0',
			type: 'Feature',
			geometry: {
				type: 'Polygon',
				coordinates: [
					[
						[50.551620691, 29.749561352],
						[53.354497121, 20.109526858],
						[64.17384073, 19.552776521],
						[67.673225938, 27.247511515],
						[60.654005351, 33.282483434],
						[50.551620691, 29.749561352]
					]
				]
			},
			properties: {
				mode: 'polygon'
			}
		},
		{
			id: '3f2504e0-4f89-11d3-9a0c-0305e82c3301',
			type: 'Feature',
			geometry: {
				type: 'Polygon',
				coordinates: [
					[
						[5.783825208545238, 44.24016772359042],
						[5.043914894643905, 47.96908293794456],
						[5.1111794686347025, 49.50677418549904],
						[0.649296060563131, 50.64349197692914],
						[-1.3237981098398564, 47.66797156953183],
						[1.792793818410587, 44.79969551086995],
						[3.0483991995765507, 46.554076107477385],
						[5.783825208545238, 44.24016772359042]
					]
				]
			},
			properties: {
				mode: 'polygon'
			}
		},
		{
			id: '23d1a5b7-fb80-4310-befc-d48af8d42007',
			type: 'Feature',
			geometry: {
				type: 'Polygon',
				coordinates: [
					[
						[9.095279849, 31.437112592],
						[40.664336241, 31.437112592],
						[40.664336241, 13.509421612],
						[9.095279849, 13.509421612],
						[9.095279849, 31.437112592]
					]
				]
			},
			properties: {
				mode: 'rectangle'
			}
		},
		{
			id: '7b8e9c52-17cf-4b59-8e93-91581734e729',
			type: 'Feature',
			geometry: {
				type: 'Polygon',
				coordinates: [
					[
						[47.250570848, 44.814055968],
						[47.217774222, 45.285698736],
						[47.119700192, 45.748966334],
						[46.957293265, 46.199564158],
						[46.732117509, 46.633423804],
						[46.446341489, 47.046731525],
						[46.102717384, 47.435950594],
						[45.704554483, 47.797837968],
						[45.255687311, 48.129455815],
						[44.760438706, 48.428178577],
						[44.223578182, 48.69169637],
						[43.650275998, 48.918015502],
						[43.04605337, 49.105456952],
						[42.41672929, 49.252653584],
						[41.768364494, 49.358546791],
						[41.107203089, 49.42238322],
						[40.439612421, 49.443712052],
						[39.772021753, 49.42238322],
						[39.110860348, 49.358546791],
						[38.462495552, 49.252653584],
						[37.833171472, 49.105456952],
						[37.228948844, 48.918015502],
						[36.65564666, 48.69169637],
						[36.118786136, 48.428178577],
						[35.623537531, 48.129455815],
						[35.174670359, 47.797837968],
						[34.776507458, 47.435950594],
						[34.432883353, 47.046731525],
						[34.147107333, 46.633423804],
						[33.921931577, 46.199564158],
						[33.75952465, 45.748966334],
						[33.66145062, 45.285698736],
						[33.628653994, 44.814055968],
						[33.66145062, 44.338524103],
						[33.75952465, 43.863739711],
						[33.921931577, 43.394442949],
						[34.147107333, 42.935425254],
						[34.432883353, 42.491472423],
						[34.776507458, 42.067304097],
						[35.174670359, 41.667510838],
						[35.623537531, 41.296490149],
						[36.118786136, 40.958382857],
						[36.65564666, 40.657011328],
						[37.228948844, 40.395820984],
						[37.833171472, 40.177826484],
						[38.462495552, 40.005563864],
						[39.110860348, 39.881049765],
						[39.772021753, 39.80574869],
						[40.439612421, 39.780549073],
						[41.107203089, 39.80574869],
						[41.768364494, 39.881049765],
						[42.41672929, 40.005563864],
						[43.04605337, 40.177826484],
						[43.650275998, 40.395820984],
						[44.223578182, 40.657011328],
						[44.760438706, 40.958382857],
						[45.255687311, 41.296490149],
						[45.704554483, 41.667510838],
						[46.102717384, 42.067304097],
						[46.446341489, 42.491472423],
						[46.732117509, 42.935425254],
						[46.957293265, 43.394442949],
						[47.119700192, 43.863739711],
						[47.217774222, 44.338524103],
						[47.250570848, 44.814055968]
					]
				]
			},
			properties: {
				mode: 'circle',
				radiusKilometers: 555.9527304732782
			}
		},
		{
			id: '72734476-2bab-4c12-a9eb-e52fa1f4793f',
			type: 'Feature',
			geometry: {
				type: 'Polygon',
				coordinates: [
					[
						[35.938168816, -11.070990442],
						[35.987992827, -11.200358692],
						[36.036752204, -11.330058792],
						[36.084443775, -11.460081154],
						[36.131064442, -11.590416166],
						[36.176611174, -11.721054196],
						[36.22108101, -11.851985597],
						[36.26447106, -11.9832007],
						[36.306778503, -12.114689824],
						[36.348000589, -12.24644327],
						[36.388134639, -12.378451325],
						[36.427178045, -12.510704266],
						[36.465128267, -12.643192355],
						[36.50198284, -12.775905846],
						[36.537739368, -12.908834981],
						[36.572395526, -13.041969996],
						[36.605949062, -13.17530112],
						[36.638397794, -13.308818573],
						[36.669739615, -13.442512575],
						[36.699972486, -13.576373338],
						[36.729094441, -13.710391073],
						[36.75710359, -13.844555992],
						[36.783998109, -13.978858303],
						[36.809776253, -14.113288219],
						[36.834436343, -14.247835951],
						[36.857976779, -14.382491717],
						[36.88039603, -14.517245738],
						[36.901692638, -14.65208824],
						[36.921865219, -14.787009458],
						[36.940912462, -14.921999632],
						[36.958833128, -15.057049013],
						[36.975626054, -15.192147863],
						[36.991290147, -15.327286453],
						[37.005824389, -15.462455068],
						[37.019227836, -15.597644008],
						[37.031499616, -15.732843584],
						[37.042638931, -15.868044127],
						[37.052645057, -16.003235983],
						[37.061517345, -16.138409516],
						[37.069255217, -16.273555109],
						[37.07585817, -16.408663166],
						[37.081325776, -16.543724113],
						[37.085657678, -16.678728397],
						[37.088853596, -16.81366649],
						[37.09091332, -16.948528886],
						[37.091836719, -17.083306108],
						[37.09162373, -17.217988704],
						[37.09027437, -17.352567249],
						[37.087788724, -17.487032348],
						[37.084166955, -17.621374635],
						[37.079409298, -17.755584775],
						[37.073516062, -17.889653464],
						[37.066487631, -18.023571432],
						[37.05832446, -18.157329442],
						[37.049027082, -18.290918291],
						[37.0385961, -18.424328813],
						[37.027032192, -18.557551877],
						[37.01433611, -18.690578389],
						[37.000508679, -18.823399296],
						[36.985550799, -18.956005581],
						[36.96946344, -19.088388269],
						[36.952247649, -19.220538425],
						[36.933904545, -19.352447156],
						[36.914435321, -19.484105612],
						[36.893841241, -19.615504985],
						[45.818019385, -20.886549176],
						[45.849246396, -20.688882043],
						[45.878767776, -20.490727647],
						[45.906581609, -20.292100036],
						[45.932686086, -20.093013365],
						[45.95707951, -19.893481897],
						[45.979760295, -19.693519998],
						[46.000726967, -19.493142137],
						[46.019978164, -19.292362885],
						[46.037512634, -19.091196911],
						[46.053329237, -18.88965898],
						[46.067426945, -18.687763953],
						[46.079804842, -18.485526783],
						[46.090462123, -18.282962513],
						[46.099398096, -18.080086276],
						[46.106612179, -17.876913288],
						[46.112103904, -17.67345885],
						[46.115872914, -17.469738345],
						[46.117918963, -17.265767235],
						[46.118241919, -17.061561057],
						[46.116841761, -16.857135422],
						[46.11371858, -16.652506014],
						[46.108872578, -16.447688586],
						[46.102304071, -16.242698956],
						[46.094013486, -16.037553006],
						[46.084001361, -15.832266681],
						[46.072268348, -15.626855983],
						[46.058815209, -15.42133697],
						[46.043642819, -15.215725754],
						[46.026752163, -15.010038496],
						[46.00814434, -14.804291406],
						[45.987820559, -14.598500739],
						[45.965782141, -14.392682791],
						[45.94203052, -14.186853897],
						[45.916567238, -13.981030431],
						[45.88939395, -13.775228799],
						[45.860512424, -13.569465435],
						[45.829924537, -13.363756806],
						[45.797632276, -13.1581194],
						[45.763637741, -12.952569728],
						[45.727943141, -12.747124321],
						[45.690550797, -12.541799725],
						[45.65146314, -12.3366125],
						[45.610682709, -12.131579216],
						[45.568212156, -11.926716451],
						[45.524054241, -11.722040785],
						[45.478211835, -11.517568803],
						[45.430687917, -11.313317085],
						[45.381485578, -11.10930221],
						[45.330608013, -10.905540746],
						[45.278058532, -10.702049254],
						[45.22384055, -10.498844281],
						[45.16795759, -10.295942356],
						[45.110413286, -10.093359991],
						[45.051211378, -9.891113675],
						[44.990355714, -9.689219874],
						[44.92785025, -9.487695025],
						[44.863699048, -9.286555534],
						[44.797906279, -9.085817775],
						[44.730476219, -8.885498086],
						[44.661413252, -8.685612766],
						[44.590721866, -8.486178072],
						[44.518406656, -8.287210216],
						[44.444472324, -8.088725366],
						[44.368923674, -7.890739638],
						[35.938168816, -11.070990442]
					]
				]
			},
			properties: {
				mode: 'sensor'
			}
		},
		{
			id: 'e63a579a-e6e2-469d-a1d8-962455328aa1',
			type: 'Feature',
			geometry: {
				type: 'Polygon',
				coordinates: [
					[
						[15.454314804, -12.104461056],
						[28.141841716, -2.845012842],
						[27.999415969, -2.654973749],
						[27.85413742, -2.467072752],
						[27.706039106, -2.281355562],
						[27.555154704, -2.097867257],
						[27.401518527, -1.916652279],
						[27.245165512, -1.737754416],
						[27.086131215, -1.561216797],
						[26.924451799, -1.38708188],
						[26.760164033, -1.215391442],
						[26.593305274, -1.046186575],
						[26.423913468, -0.87950767],
						[26.252027136, -0.715394417],
						[26.077685363, -0.55388579],
						[25.900927798, -0.395020048],
						[25.721794634, -0.238834722],
						[25.540326607, -0.085366613],
						[25.356564985, 0.065348218],
						[25.170551554, 0.213274449],
						[24.982328615, 0.358377509],
						[24.791938971, 0.500623577],
						[24.599425917, 0.639979589],
						[24.404833231, 0.776413242],
						[24.208205164, 0.909892995],
						[24.009586431, 1.040388074],
						[23.809022197, 1.167868475],
						[23.606558072, 1.292304967],
						[23.402240097, 1.413669093],
						[23.196114735, 1.531933171],
						[22.988228858, 1.647070299],
						[22.778629743, 1.759054353],
						[22.567365051, 1.86785999],
						[22.354482825, 1.973462649],
						[22.140031475, 2.075838553],
						[21.92405977, 2.174964706],
						[21.70661682, 2.270818896],
						[21.487752074, 2.363379696],
						[21.267515302, 2.452626463],
						[21.045956587, 2.538539336],
						[20.823126312, 2.62109924],
						[20.59907515, 2.700287882],
						[20.37385405, 2.776087755],
						[20.147514229, 2.848482132],
						[19.920107157, 2.917455069],
						[19.691684547, 2.982991404],
						[19.462298344, 3.045076757],
						[19.232000711, 3.103697527],
						[19.000844018, 3.158840894],
						[18.768880832, 3.210494816],
						[18.536163901, 3.258648032],
						[18.302746146, 3.303290054],
						[18.068680647, 3.344411177],
						[17.834020633, 3.382002466],
						[17.598819464, 3.416055767],
						[17.363130627, 3.446563698],
						[17.127007718, 3.473519652],
						[16.890504432, 3.496917798],
						[16.653674552, 3.516753075],
						[16.416571933, 3.533021197],
						[16.179250494, 3.545718651],
						[15.941764201, 3.554842695],
						[15.70416706, 3.560391361],
						[15.466513102, 3.562363451],
						[15.228856371, 3.560758539],
						[14.991250909, 3.555576972],
						[15.454314804, -12.104461056]
					]
				]
			},
			properties: {
				mode: 'sector'
			}
		},
		{
			id: 'c4a0811b-9326-46cb-8a7c-0e3df3de42f4',
			type: 'Feature',
			geometry: {
				type: 'Polygon',
				coordinates: [
					[
						[-3.430816986, 10.463562566],
						[-3.430816986, 10.463562566],
						[-3.430816986, 10.463562566],
						[-14.949075276, 5.862551468],
						[-12.913681749, 18.619457771],
						[-19.101663017, 19.414420429],
						[-20.624599309, 25.140673828],
						[-14.045658882, 27.008765],
						[-8.101876719, 25.461502677],
						[-4.92728835, 30.184129589],
						[-0.140745297, 26.275467203],
						[2.692204914, 20.957788993],
						[-3.430816986, 10.463562566]
					]
				]
			},
			properties: {
				mode: 'freehand'
			}
		}
	];

	return {
		title: 'Maplibre GL Terra Draw',
		description:
			'This plugin is to add controls to your MapLibre for sketching powered by Terra Draw library.',
		socialImage: '/assets/plugin-overview.png',
		codes: {
			cdn: await getExample('cdn'),
			npm: await getExample('npm')
		},
		geojson
	};
};
